#include "nvs_flash.h" 
#include "m_ble.h"

void nvs_write_ble_param_information_to_flash(ble_esp32_param_t* param)
{
	nvs_handle handle;
	static const char *INFORMATION_DATA = "ble_param";

	ESP_ERROR_CHECK( nvs_open( INFORMATION_DATA, NVS_READWRITE, &handle) );
	ESP_ERROR_CHECK( nvs_set_blob( handle, INFORMATION_DATA, param, sizeof(ble_esp32_param_t)));
	ESP_ERROR_CHECK( nvs_commit(handle) );
    nvs_close(handle);
}

uint8_t  nvs_read_ble_param_information(ble_esp32_param_t* param)
{
	nvs_handle handle;
	static const char *INFORMATION_DATA = "ble_param";
    uint8_t ready_to_read_num = sizeof(ble_esp32_param_t);
	esp_err_t err;
	
	 err =  nvs_open(INFORMATION_DATA, NVS_READONLY, &handle);                     
	if(err != ESP_OK )
	{
		return 0xFF ;
	}

	err = nvs_get_blob(handle, INFORMATION_DATA, param, &ready_to_read_num) ;
	if(err != ESP_OK )
	{
		return 0xFF ;
	}
	 nvs_close(handle);
	return 0x01;
}

void clear_wifi_configed_param(void)
{
	// wifi_config_t wifi_configed_param;
	// uint8_t configed_flag=0;
	
	// bzero(&wifi_configed_param, sizeof(wifi_configed_param));			//clear param
	
	// nvs_write_data_to_flash( wifi_configed_param,  configed_flag );		//write param to flash
}

void HexArrayToString(unsigned char *hexarray,int length, char *string)
{
	for(int i=0; i < length; i++)
	{
		switch(hexarray[i])
		{
			case 0x2E:
			{
				*(string++) = '.';
			}break;
			case 0x30:
			{
				*(string++) = '0';
			}break;
			case 0x31:
			{
				*(string++) = '1';
			}break;
			case 0x32:
			{
				*(string++) = '2';
			}break;
			case 0x33:
			{
				*(string++) = '3';
			}break;
			case 0x34:
			{
				*(string++) = '4';
			}break;
			case 0x35:
			{
				*(string++) = '5';
			}break;
			case 0x36:
			{
				*(string++) = '6';
			}break;
			case 0x37:
			{
				*(string++) = '7';
			}break;
			case 0x38:
			{
				*(string++) = '8';
			}break;
			case 0x39:
			{
				*(string++) = '9';
			}break;
		}
	}
	*string = 0x0;
}
